package com.qzsoft.zhjd.materiel.modules.materieldatarecive.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.utils.ConvertDictUtils;
import com.qzsoft.jeemis.common.utils.ConvertUtils;
import com.qzsoft.zhjd.common.annotation.ZhjdDataFilter;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.common.excel.ZhjdExcelExportUtils;
import com.qzsoft.zhjd.common.excel.style.ZhjdExcelExportStyler;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataListDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.enmus.WeightTypeEnum;
import com.qzsoft.zhjd.materiel.modules.materieldata.service.MaterielDataService;
import com.qzsoft.zhjd.materiel.modules.materieldatarecive.dto.ExportReceiveBookExcelDto;
import com.qzsoft.zhjd.materiel.modules.materieldatarecive.dto.MaterielReceiveForCategoryDto;
import com.qzsoft.zhjd.materiel.modules.materieldatarecive.dto.MaterielReceiveForItemDto;
import com.qzsoft.zhjd.materiel.modules.materieldatarecive.mapper.MaterielDataReceiveMapper;
import com.qzsoft.zhjd.materiel.modules.materieldatarecive.service.MaterielDataReceiveService;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName MaterielDataReceiveServiceImpl
 * @Description 收料数据业务接口实现
 * @author linhuawei
 * @since 2022-03-31
 */
@Slf4j
@Service
public class MaterielDataReceiveServiceImpl implements MaterielDataReceiveService {

    @Autowired
    private MaterielDataService materielDataService;

    @Autowired
    private MaterielDataReceiveMapper materielDataReceiveMapper;

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Override
    public void exportBook(Map<String, Object> params, HttpServletResponse response) {
        params.put("weightType", WeightTypeEnum.RECEIVE.getValue());
        List<MaterielDataListDto> sourceList = materielDataService.ListDto(params);

        List<ExportReceiveBookExcelDto> list = ConvertUtils.sourceToTarget(sourceList, ExportReceiveBookExcelDto.class);
        String fileName = "收料明细台账";
        ExportParams exportParams = new ExportParams(null, fileName);
        exportParams.setFixedTitle(false);
        exportParams.setStyle(ZhjdExcelExportStyler.class);
        try {
            ZhjdExcelExportUtils.exportExcel(response, fileName, exportParams, ExportReceiveBookExcelDto.class, list);
        } catch (IOException e) {
            log.error("收料明细台账导出失败：{}", e);
            RenExceptionUtils.throwEx("数据导出失败");
        }
    }

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public IPage<MaterielReceiveForCategoryDto> countForCategory(Map<String, Object> params) {
        IPage<MaterielReceiveForCategoryDto> page = baseServiceComponent.getPage(params,
                MaterielReceiveForCategoryDto.class);
        IPage<MaterielReceiveForCategoryDto> dataPage = materielDataReceiveMapper.countForCategory(page, params);
        return ConvertDictUtils.formatDicDataPage(dataPage);
    }

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public IPage<MaterielReceiveForItemDto> countForItem(Map<String, Object> params) {
        IPage<MaterielReceiveForItemDto> page = baseServiceComponent.getPage(params, MaterielReceiveForItemDto.class);
        IPage<MaterielReceiveForItemDto> dataPage = materielDataReceiveMapper.countForItem(page, params);
        return ConvertDictUtils.formatDicDataPage(dataPage);
    }

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public void countExport(Map<String, Object> params, HttpServletResponse response) {
        // 获取按照分类统计
        List<MaterielReceiveForCategoryDto> categoryList = materielDataReceiveMapper.countForCategory(params);
        categoryList = ConvertDictUtils.formatDicDataList(categoryList);
        ExportParams categoryParams = new ExportParams();
        categoryParams.setSheetName("按材料分类");
        categoryParams.setFixedTitle(false);
        categoryParams.setStyle(ZhjdExcelExportStyler.class);
        Map<String, Object> categoryMap = new HashMap<String, Object>();
        categoryMap.put("title", categoryParams);
        categoryMap.put("entity", MaterielReceiveForCategoryDto.class);
        categoryMap.put("data", categoryList);

        // 获取按照材料统计
        List<MaterielReceiveForItemDto> itemList = materielDataReceiveMapper.countForItem(params);
        itemList = ConvertDictUtils.formatDicDataList(itemList);
        ExportParams itemParams = new ExportParams();
        itemParams.setSheetName("按材料明细");
        itemParams.setFixedTitle(false);
        itemParams.setStyle(ZhjdExcelExportStyler.class);
        Map<String, Object> itemMap = new HashMap<String, Object>();
        itemMap.put("title", itemParams);
        itemMap.put("entity", MaterielReceiveForItemDto.class);
        itemMap.put("data", itemList);

        // 导出文件sheet列表
        List<Map<String, Object>> sheets = new ArrayList<Map<String, Object>>();
        sheets.add(categoryMap);
        sheets.add(itemMap);

        // 数据导出
        String fileName = "收料汇总分析";
        try {
            ZhjdExcelExportUtils.exportExcel(response, fileName, sheets);
        } catch (IOException e) {
            log.error("收料明细台账导出失败：{}", e);
            RenExceptionUtils.throwEx("数据导出失败");
        }
    }

}
